package cn.stylefeng.guns.sys.modular.plotHouseInfo.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.importHelper.*;
import cn.stylefeng.guns.sys.modular.personInfo.model.params.PersonInfoParam;
import cn.stylefeng.guns.sys.modular.personInfo.service.PersonInfoService;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.entity.HouseInfo;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.TreeBean;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.params.HouseInfoParam;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.result.HouseInfoResult;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.service.HouseInfoService;
import cn.stylefeng.guns.sys.modular.plotInfo.model.params.XqInfoParam;
import cn.stylefeng.guns.sys.modular.plotInfo.model.result.XqInfoResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 房产表控制器
 *
 * @author WangZhen
 * @Date 2021-01-21 11:29:32
 */
@Controller
@RequestMapping("/houseInfo")
public class HouseInfoController extends BaseController {

    private String PREFIX = "/modular/plotHouseInfo/houseInfo";

    @Autowired
    private HouseInfoService houseInfoService;

    @Autowired
    private PersonInfoService personInfoService;

    /**
     * 跳转到主页面
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/houseInfo.html";
    }

    /**
     * 新增页面
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/houseInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/houseInfo_edit.html";
    }

    /**
     * 新增接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HouseInfoParam houseInfoParam) {
        this.houseInfoService.add(houseInfoParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HouseInfoParam houseInfoParam) {
        this.houseInfoService.updatePlotHouseInfo(houseInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HouseInfoParam houseInfoParam) {
        this.houseInfoService.delete(houseInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HouseInfoParam houseInfoParam) {
        HouseInfoResult detail = this.houseInfoService.findListBySpec(houseInfoParam).get(0);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HouseInfoParam houseInfoParam) {
        return this.houseInfoService.findPageBySpec(houseInfoParam);
    }

    /**
     * 查询列表
     *
     * @author author
     * @Date 2021-01-06
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<HouseInfoResult> listAll(HouseInfoParam houseInfoParam) {
        return this.houseInfoService.findListBySpec(houseInfoParam);
    }

    /**
     * 查询列表
     *
     * @author author
     * @Date 2021-01-06
     */
    @ResponseBody
    @RequestMapping("/listHouseTree")
    public ResponseData listHouseTree(TreeBean treeBean) {
        return this.houseInfoService.listTree(treeBean);
    }

    /**
     * 删除接口
     */
    @ResponseBody
    @RequestMapping("/allDelete")
    public Integer allDelete(@RequestParam Integer[] ids) {
        Integer returnNumber = 0;
        for (int i = 0; i < ids.length; i++) {
            this.houseInfoService.selectDeleteInfo(ids[i]);
            returnNumber++;
        }
        return returnNumber;
    }

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 导入数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/importData_new")
    public ResponseData importData_new(MultipartFile file, String relXqId) throws IOException {
        List listHouseParam = new ArrayList();
        List listHouseId = new ArrayList();
        List listPersonParam = new ArrayList();
        List listPersonId = new ArrayList();
        String userName = LoginContextHolder.getContext().getUser().getName();
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        ExcelConfig excelConfig = new ExcelConfig(file, 0, 1, -1, 0, 3, "project_wxjj.wxjj_house_info");
        ExcelConfig excelConfigPerson = new ExcelConfig(file, 0, 1, -1, 4, 9, "project_wxjj.wxjj_person_info");
        ExcelConfig excelConfigPersonAndHouse = new ExcelConfig(file, 0, 1, -1, 5, 6, "project_wxjj.wxjj_person_house_rel");
        excelConfig.setBatchSize(100);
        excelConfigPerson.setBatchSize(100);
        excelConfigPersonAndHouse.setBatchSize(100);
        IBaseExcel houseImport = new BaseExcelHandle() {

            @Override
            protected int checkRow(Object[] row, DataStore data, IContext context) {
                // TODO Auto-generated method stub
                HouseInfoParam houseInfoParam = new HouseInfoParam();
                Object[] temp = data.getData();
                temp[0] = null;
                temp[1] = "0";
                temp[2] = row[0];
                temp[3] = row[1];
                temp[4] = row[2];
                temp[5] = row[3];
                temp[6] = relXqId;
                temp[7] = userName;
                temp[8] = createTime;
                data.setData(temp);
                houseInfoParam.setIsDel(0);
                houseInfoParam.setBuildNo((String) row[0]);
                houseInfoParam.setUnitNo((String) row[1]);
                houseInfoParam.setLevelNo((String) row[2]);
                houseInfoParam.setDoorNo((String) row[3]);
                houseInfoParam.setRelXqId(Integer.parseInt(relXqId));
                listHouseParam.add(houseInfoParam);
                return BaseExcelHandle.GO_CONTINUE;
            }

            @Override
            protected Object beforeHandle() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected Object afterHandle() {
                // TODO Auto-generated method stub
                for (int i = 0; i < listHouseParam.size(); i++) {
                    listHouseId.add(houseInfoService.findListBySpecSingle((HouseInfoParam) listHouseParam.get(i)).get(0).getId());
                }
                return null;
            }

        };
        houseImport.setSqlSessionTemplate(sqlSessionTemplate);
        houseImport.run(excelConfig);


        IBaseExcel personImport = new BaseExcelHandle() {

            @Override
            protected int checkRow(Object[] row, DataStore data, IContext context) {
                // TODO Auto-generated method stub
                PersonInfoParam personInfoParam = new PersonInfoParam();
                Object[] temp = data.getData();
                temp[0] = null;
                temp[1] = "0";
                temp[2] = row[4];
                temp[3] = row[9];
                temp[4] = "111111";
                temp[5] = row[9];
                temp[6] = row[7];
                temp[7] = row[8];
                temp[8] = userName;
                temp[9] = createTime;
                data.setData(temp);
                personInfoParam.setIsDel(0);
                personInfoParam.setPersonName((String) row[4]);
                personInfoParam.setUserName((String) row[9]);
                personInfoParam.setPwd("111111");
                personInfoParam.setPhone((String) row[9]);
                personInfoParam.setCardType((String) row[7]);
                personInfoParam.setCardCode((String) row[8]);
                listPersonParam.add(personInfoParam);
                return BaseExcelHandle.GO_CONTINUE;
            }

            @Override
            protected Object beforeHandle() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected Object afterHandle() {
                // TODO Auto-generated method stub
                for (int i = 0; i < listPersonParam.size(); i++) {
                    listPersonId.add(personInfoService.findListBySpec((PersonInfoParam) listPersonParam.get(i)).get(0).getId());
                }
                return null;
            }

        };
        personImport.setSqlSessionTemplate(sqlSessionTemplate);
        personImport.run(excelConfigPerson);


        IBaseExcel personAndHouseImport = new BaseExcelHandle() {
            Integer i = 0;

            @Override
            protected int checkRow(Object[] row, DataStore data, IContext context) {
                // TODO Auto-generated method stub
                if (i < listHouseId.size()) {
                    Object[] temp = data.getData();
                    temp[0] = null;
                    temp[1] = "0";
                    temp[2] = listPersonId.get(i).toString();
                    temp[3] = listHouseId.get(i).toString();
                    if (row[5].equals("业主")) {
                        temp[4] = "1";
                    } else {
                        temp[4] = "2";
                    }
                    if (row[6].equals("本人")) {
                        temp[5] = "1";
                    } else {
                        temp[5] = "2";
                    }
                    temp[6] = null;
                    temp[7] = null;
                    temp[8] = null;
                    temp[9] = userName;
                    temp[10] = createTime;
                    data.setData(temp);
                }
                i++;
                return BaseExcelHandle.GO_CONTINUE;
            }

            @Override
            protected Object beforeHandle() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected Object afterHandle() {
                // TODO Auto-generated method stub
                return null;
            }

        };
        personAndHouseImport.setSqlSessionTemplate(sqlSessionTemplate);
        personAndHouseImport.run(excelConfigPersonAndHouse);
        return ResponseData.success(400, "导入成功", null);

    }

}


